<?php
namespace Home\Model;
use Think\Model;

class StationInfoModel extends BaseModel {

	protected $tableName = 'tp_station_info';

	/**
	 * @desc: 修改站点信息
	 * @Author: delayhsiao
	 * @update: 2016-11-23
	 **/
	public function updateStationInfo($data){
		switch($data['updateType']){
			case 0:
				$save = array(
					'f_sms_notice' => $data['sms'],
					'f_weixin_notice' => $data['weixin'],
					'f_app_notice' => $data['app']
				);
				break;
			case 1:
				$save = array(
					'f_store_time' => $data['storeTime']
				);
				break;
		}
		$result = $this->where(array('f_station_id' => $data['stationId']))->save($save);
		if($result !== false){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * @desc 获取通知方式模型
	 * @author quyang
	 * @update 2016-11-24
	 */
	public function stationinfo($where){
	    $data = $this->where($where)
	                   ->field('
                         f_app_notice as app,
                         f_weixin_notice as weixin,
                         f_sms_notice as sms,
                         f_station_name as stationName,
                         f_store_time as storeTime,
                         f_station_id as stationId
                         ')->find();
	    return $data;
	}

	/**
	 * 查询附近站点列表数据
	 * @author cheng
	 */
	public function getNearbyStation($map, $page, $nums) {
//			print_r($map);
		$count = $this->alias('s')
			->field('count(DISTINCT(s.f_station_id)) as totals')
			->join('left join tp_users_additional uad on uad.f_user_id = s.f_station_id')
			->join('left join tp_bg_verify_attchment va on va.f_verify_id = uad.f_addition_id')
			->join('left join tp_bg_attachment a on a.f_attr_id = va.f_attr_id')
			->join('left join tp_user u on u.f_user_id = s.f_station_id')
			->join('left join tp_user_address ua ON ua.f_user_id = s.f_station_id')
			->where($map)
			->find();
		//echo M()->_sql();exit();
		$list = $this->alias('s')
			->field('s.f_station_id as stationId,s.f_station_name as stationName,s.f_store_time storeTime,s.f_user_id as userId,case count(a.f_attr_id)  when 3 then max(a.f_url) else "" END As url,ua.f_address as address,u.f_user_mobile as mobile,u.f_user_phone as phone')
			->join('left join tp_users_additional uad on uad.f_user_id = s.f_station_id')
			->join('left join tp_bg_verify_attchment va on va.f_verify_id = uad.f_addition_id')
			->join('left join tp_bg_attachment a on a.f_attr_id = va.f_attr_id')
			->join('left join tp_user u on u.f_user_id = s.f_station_id')
			->join('left join tp_user_address ua ON ua.f_user_id = s.f_station_id')
			->where($map)
			->group('s.f_station_id')
			->page($page, $nums)
			->select();
		//echo M()->_sql();exit();
                foreach ($list as $k => $v) {
                    $list[$k]['url'] = $v['url'] ? $v['url'] : C('IMG_URL').'static/defaultStation.jpg';
                }
		$data['totalPage'] = ceil($count['totals'] / $nums);
		$data['totalCount'] = $count['totals'];
		$data['stationList'] = $list;
		return $data;
	}
    
	/**
	 * @desc: 获取站点信息
	 * @Author: delayhsiao
	 * @update: 2016-11-29
	 **/
	public function getStationInfo($stationId){
		$station = $this->alias('s')
					->join('tp_user_address ua ON s.f_user_id = ua.f_user_id','LEFT')
					->join('tp_user u ON s.f_user_id = u.f_user_id','LEFT')
					->where(array('s.f_station_id'=>$stationId))
					->field('
						ua.f_address,
						s.f_store_time,
						u.f_user_phone,
						u.f_user_mobile
					')
					->find();
		return $station;
	}

}
